package com.alfred.mall.order.biz.service;

import com.alfred.common.framwork.util.ServiceExceptionUtil;
import com.alfred.mall.order.api.CardService;
import com.alfred.mall.order.api.bo.CalcOrderPriceBO;
import com.alfred.mall.order.api.constant.OrderErrorCodeEnum;
import com.alfred.mall.order.api.dto.CalcOrderPriceDTO;
import com.alfred.mall.product.api.ProductSpuService;
import com.alfred.mall.product.api.bo.ProductSkuDetailBO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@org.apache.dubbo.config.annotation.Service(validation = "true",version = "${dubbo.provider.CardService.version}")
public class CardServiceImpl implements CardService {

    @Reference(validation = "true", version = "${dubbo.consumer.ProductSpuService.version}")
    ProductSpuService productSpuService;

    @Override
    public CalcOrderPriceBO calcOrderPrice(CalcOrderPriceDTO calcOrderPriceDTO) {
        Map<Integer, CalcOrderPriceDTO.Item> calcOrderItemMap = calcOrderPriceDTO.getItems().stream()
                .collect(Collectors.toMap(CalcOrderPriceDTO.Item::getSkuId, item -> item));
        Set<Integer> integers = calcOrderItemMap.keySet();
        List<ProductSkuDetailBO> skus = productSpuService.getProductSkuDetailList(integers);
        if (skus.size()!=calcOrderPriceDTO.getItems().size()){
            throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.ORDER_ITEM_SOME_NOT_EXISTS.getCode());
        }
        return null;
    }
}
